
from dtnmanagement import dtnmanager
from httpsqs_client import httpsqs
import time
import os

req_sqs = httpsqs('127.0.0.1', '1218', 'php_python_request')
reply_sqs = httpsqs('127.0.0.1', '1218', 'php_python_reply')
dm = dtnmanager('127.0.0.1')

DONE = 'ACK'
NOTDONE = 'NACK'

if __name__ == "__main__":
	cpid = os.fork()
	if cpid == 0:
		while(1):
			msg = req_sqs.get()
			if msg != False:
				if msg == "start":
					child_pid = os.fork()
					if child_pid == 0:
						dm.start_dtn()
					else:
						#print os.getpid()
						reply_sqs.put(DONE)
				elif msg == "stop":
					ret = dm.stop_dtn(os.getpid())
					if ret == True:
						reply_sqs.put(DONE)
					else:
						reply_sqs.put(NOTDONE)
					break;
				else:
					info = dm.excute(msg)
					if info != False:
						reply_sqs.put(info)
					else:
						reply_sqs.put(NOTDONE)
			else:
				time.sleep(1)
	else:
		exit(0)
